//
//  KMContext.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 ZHBen. All rights reserved.
//

#import "KMContext.h"
#import "KDDCollection.h"
//#import "KMStorageCenter.h"
#import "KMConfigStore.h"
#import "KMStorage.h"

@interface KUIModule (KMContext)

- (void)setContext:(KMContext *)context;

@end

@implementation KMContext
{
    KUIModule * _module;
}

@dynamic updateImmediate;
@dynamic reloadNextUpdate;

+ (KMContext *)contextWithFrame:(CGRect)frame displayInsets:(UIEdgeInsets)displayInsets collectionViewLayout:(KDDCollectionLayout *)layout
{
    KDDCollectionView * collectionView = [[KDDCollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
    collectionView.displayInsets = displayInsets;
    return [[self alloc] initWithCollectionView:collectionView];
}

+ (KMContext *)contextWithFrame:(CGRect)frame displayInsets:(UIEdgeInsets)displayInsets
{
    KDDCollectionView * collectionView = [[KDDCollectionView alloc] initWithFrame:frame];
    collectionView.displayInsets = displayInsets;
    return [[self alloc] initWithCollectionView:collectionView];
}

- (instancetype)initWithCollectionView:(KDDCollectionView *)collectionView
{
    if (self = [super init])
    {
        KMStorage * storage = [KMStorage new];
        storage.minUpdateInterval = .2;
        _dataDriven = [[KDDCollection alloc] initWithStorage:storage];
        [(KDDCollection *)_dataDriven useSharedConfigure];
        [_dataDriven applyToLayoutView:collectionView];
        _collectionView = collectionView;
        _module = [[KUIModule alloc] initWithStorage:storage];
        _module.context = self;
    }
    return self;
}

- (void)setScrollDelegate:(id<UIScrollViewDelegate>)delegate
{
    self.dataDriven.additionalDelegate = delegate;
}

- (NSArray<KUIModule *> *)moduleList
{
    return _module.moduleList;
}

- (void)appendModuleList:(NSArray<KUIModule *> *)moduleList
{
    [self insertModuleList:moduleList atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(_module.moduleList.count, moduleList.count)]];
}

- (void)insertModuleList:(NSArray<KUIModule *> *)moduleList atIndexes:(NSIndexSet *)indexSet
{
    [_module insertModuleList:moduleList atIndexes:indexSet];
}

- (void)deleteModuleListAtIndexes:(NSIndexSet *)indexSet
{
//    [self uninstallModuleList:[self.moduleList objectsAtIndexes:indexSet]];
//    [_moduleList removeObjectsAtIndexes:indexSet];
//    [_storageCenter deleteStorageListAtIndexes:indexSet];
    
    [_module deleteModuleListAtIndexes:indexSet];
}

- (void)modifyModuleList:(NSArray<KUIModule *> *)moduleList atIndexes:(NSIndexSet *)indexSet
{
//    [self uninstallModuleList:[self.moduleList objectsAtIndexes:indexSet]];
//    [_moduleList replaceObjectsAtIndexes:indexSet withObjects:moduleList];
//    [_storageCenter modifyStorageList:[moduleList valueForKeyPath:@"storage"] atIndexes:indexSet];
//    [self installModuleList:moduleList];
    
    [_module modifyModuleList:moduleList atIndexes:indexSet];
}

- (void)reloadModuleList:(NSArray<KUIModule *> *)moduleList
{
//    [self uninstallModuleList:self.moduleList];
//    _moduleList = [NSMutableArray arrayWithArray:moduleList];
//    [_storageCenter reloadStorageList:[moduleList valueForKeyPath:@"storage"]];
//    [self installModuleList:moduleList];
    
    [_module reloadModuleList:moduleList];
}

- (NSArray *)visibleModelsOfModule:(KUIModule *)module visibleViews:(NSArray<__kindof UIView<KMView> *> **)visibleViews
{
    NSArray<KDDPosition *> * positionList = [self.dataDriven visiblePositionList];
    NSArray<KDDPosition *> * modulePositions = [self positionListOfModule:module ofPositionList:positionList];
    NSArray * modelList = [self.dataDriven.storage modelsAtPositions:modulePositions notFound:nil];
    if (visibleViews != nil)
    {
        NSMutableArray<__kindof UIView<KMView> *> * viewList = [NSMutableArray arrayWithCapacity:modelList.count];
        for (KDDPosition * position in modulePositions)
        {
            UIView<KMWrapperView> * view = [self.dataDriven wrapperViewAtPosition:position];
            if (view.kmView != nil)
            {
                [viewList addObject:view.kmView];
            }
        }
        *visibleViews = viewList;
    }
    return modelList;
}

- (NSArray<KDDPosition *> *)positionListOfModule:(KUIModule *)module ofPositionList:(NSArray<KDDPosition *> *)positionList
{
    KMStorage * storage = module.storage;
    NSMutableArray<KDDPosition *> * positionList2 = [NSMutableArray array];
    NSInteger physicalSectionOffset = [module.storage physicalSectionOffset];
    for (KDDPosition * position in positionList)
    {
        if (position.section >= physicalSectionOffset && position.section < physicalSectionOffset + storage.data.count)
        {
            [positionList2 addObject:position];
        }
    }
    return positionList2;
}

- (BOOL)hasPendingUpdate
{
    return [_module.storage hasUncommittedUpdate];
}

- (void)setUpdateImmediate:(BOOL)updateImmediate
{
    _module.storage.minUpdateInterval = updateImmediate ? 0 : 0.2;
//    _storageCenter.updateImmediate = updateImmediate;
}

- (BOOL)updateImmediate
{
    return _module.storage.minUpdateInterval == 0;
//    return _storageCenter.updateImmediate;
}

- (void)setReloadNextUpdate:(BOOL)reloadNextUpdate
{
    _module.storage.reloadNextUpdate = reloadNextUpdate;
//    _storageCenter.reloadNextUpdate = reloadNextUpdate;
}

- (BOOL)reloadNextUpdate
{
//    return _storageCenter.reloadNextUpdate;
    return _module.storage.reloadNextUpdate;
}

@end
